package com.fgs.common.widget.dialog;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.fgs.common.CommonDialog;
import com.fgs.common.R;
import com.xuexiang.xui.widget.dialog.BaseDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

/**
 * @author fgs
 * @date 2020/9/10
 * @description
 **/
public class OperateTipDialog extends CommonDialog implements View.OnClickListener {
    final Builder builder;
    protected TextView titleTextView;
    protected TextView contentTextView;
    protected EditText contentEditText;
    protected TextView positiveTextView;
    protected TextView negativeTextView;
    protected View view;

    protected OperateTipDialog(Builder builder) {
        super(builder.context, R.style.style_select_dialog);
        this.builder = builder;
        view = getLayoutInflater().inflate(DialogInit.getInflateLayout(builder), null);
        DialogInit.init(this);
    }

    @Override
    public void onClick(View v) {
        DialogAction tag = (DialogAction) v.getTag();
        switch (tag) {
            case NEGATIVE:
                if (builder.onNegativeCallback != null) {
                    builder.onNegativeCallback.onClick(this);
                }
                dismiss();
                break;
            case POSITIVE:
                if (builder.onPositiveCallback != null) {
                    builder.onPositiveCallback.onClick(this);
                }
                break;
        }
    }

    public static class Builder {

        protected Context context;
        protected String title;
        protected String content;
        protected String positiveText;
        protected String negativeText;
        protected SingleButtonCallback onPositiveCallback;
        protected SingleButtonCallback onNegativeCallback;
        protected InputCallback onInputCallback;
        protected boolean isCancelable = true;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder title(String title) {
            this.title = title;
            return this;
        }

        public Builder content(String content) {
            this.content = content;
            return this;
        }

        public Builder positiveText(String message) {
            this.positiveText = message;
            return this;
        }

        public Builder negativeText(String message) {
            this.negativeText = message;
            return this;
        }

        public Builder onPositive(SingleButtonCallback callback) {
            this.onPositiveCallback = callback;
            return this;
        }

        public Builder onNegative(SingleButtonCallback callback) {
            this.onNegativeCallback = callback;
            return this;
        }

        public Builder input(InputCallback inputCallback) {
            this.onInputCallback = inputCallback;
            return this;
        }
        public Builder input(String content,InputCallback inputCallback) {
            this.onInputCallback = inputCallback;
            this.content = content;
            return this;
        }

        public Builder isCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        private OperateTipDialog build() {
            return new OperateTipDialog(this);
        }

        public OperateTipDialog show() {
            OperateTipDialog dialog = build();
            dialog.show();
            return dialog;
        }
    }

    public String getInputContent() {
        if (contentEditText != null) {
            return contentEditText.getText().toString().trim();
        }
        return "";
    }

    public interface SingleButtonCallback {
        void onClick(@NonNull OperateTipDialog dialog);
    }

    public interface InputCallback {

        void onInput(@NonNull OperateTipDialog dialog, CharSequence input);
    }
}
